{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#人脸检测\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=22870132&client_secret=19lRSRARMngHWGwrglNyVw2oMah1zhip'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error': 'invalid_client', 'error_description': 'unknown client id'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'cached': 0,\n",
       " 'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 1575454520184,\n",
       " 'result': {'face_list': [{'angle': {'pitch': 13.65,\n",
       "     'roll': -21.04,\n",
       "     'yaw': 13.21},\n",
       "    'face_probability': 1,\n",
       "    'face_shape': {'probability': 0.7, 'type': 'oval'},\n",
       "    'face_token': '21944693d8a5aeb22785ad7e2b906d7f',\n",
       "    'face_type': {'probability': 1, 'type': 'human'},\n",
       "    'location': {'height': 178,\n",
       "     'left': 390.87,\n",
       "     'rotation': -15,\n",
       "     'top': 382.09,\n",
       "     'width': 192}}],\n",
       "  'face_num': 1},\n",
       " 'timestamp': 1603544394}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603546140711&di=399a2525b6cc04645ad9cbe56a993b86&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201803%2F17%2F20180317100748_qclgn.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype\\\"}\"\n",
    "\n",
    "access_token =  '24.bc461ac1ea96b00a47a891199a3d6592.2592000.1606127896.282335-22870043' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 9455654510165,\n",
       " 'timestamp': 1603538832,\n",
       " 'cached': 0,\n",
       " 'result': {'score': 15.38564301,\n",
       "  'face_list': [{'face_token': 'ecba5a4527a0b6b42170fc65f5ce66fc'},\n",
       "   {'face_token': '94b16e36542180404f1c31dd57e850b5'}]}}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 人脸对比\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/match\"\n",
    "\n",
    "params = \"[{\\\"image\\\": \\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603548374556&di=572bd00cf25b2a9a175371c7b338d49c&imgtype=0&src=http%3A%2F%2Fdingyue.ws.126.net%2F2020%2F0422%2F7c636463j00q95ekh0049c000xc00m8m.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"CERT\\\", \\\"quality_control\\\": \\\"LOW\\\"}, {\\\"image\\\": \\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603531227645&di=f4ba116b72f9c13d55fe0f7ed1a3e808&imgtype=0&src=http%3A%2F%2Fimgtmp.nos-jd.163yun.com%2Fimg%2Fc0VaMnFnaitYWW9SclFsMTArUE9pa1F2WVYyWG9vVW5XZUNzMGdmMWdnMWwweXRnQXR4RDZBPT0.jpg%3FimageView%26thumbnail%3D2160x0%26quality%3D90%26interlace%3D1%26type%3Djpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"LIVE\\\", \\\"quality_control\\\": \\\"LOW\\\"}]\"\n",
    "\n",
    "access_token = '24.bc461ac1ea96b00a47a891199a3d6592.2592000.1606127896.282335-22870043'  # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 8989201252019,\n",
       " 'timestamp': 1603531449,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建用户组\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/add\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group3\\\"}\"\n",
    "access_token =  '24.bc461ac1ea96b00a47a891199a3d6592.2592000.1606127896.282335-22870043' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 1011589899910, 'timestamp': 1603539016, 'cached': 0, 'result': {'face_token': 'ecba5a4527a0b6b42170fc65f5ce66fc', 'location': {'left': 505.8, 'top': 231.53, 'width': 256, 'height': 237, 'rotation': 4}}}\n"
     ]
    }
   ],
   "source": [
    "# 人脸注册\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add\"\n",
    "params = params = \"{\\\"image\\\":\\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603548374556&di=572bd00cf25b2a9a175371c7b338d49c&imgtype=0&src=http%3A%2F%2Fdingyue.ws.126.net%2F2020%2F0422%2F7c636463j00q95ekh0049c000xc00m8m.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"group_id\\\":\\\"group3\\\",\\\"user_id\\\":\\\"user1\\\",\\\"user_info\\\":\\\"abc\\\",\\\"quality_control\\\":\\\"LOW\\\"}\"\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 3510194650010,\n",
       " 'timestamp': 1603531458,\n",
       " 'cached': 0,\n",
       " 'result': {'face_token': '94648755df8961a167c9193dc82ee6a4',\n",
       "  'location': {'left': 340.67,\n",
       "   'top': 277.64,\n",
       "   'width': 206,\n",
       "   'height': 216,\n",
       "   'rotation': 3}}}"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 6565847975259,\n",
       " 'timestamp': 1603539088,\n",
       " 'cached': 0,\n",
       " 'result': {'face_list': [{'face_token': 'ecba5a4527a0b6b42170fc65f5ce66fc',\n",
       "    'ctime': '2020-10-24 19:30:17'}]}}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取用户人脸列表\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/getlist\"\n",
    "\n",
    "params = \"{\\\"user_id\\\":\\\"user1\\\",\\\"group_id\\\":\\\"group3\\\"}\"\n",
    "access_token =  '24.bc461ac1ea96b00a47a891199a3d6592.2592000.1606127896.282335-22870043'  # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 11599101946,\n",
       " 'timestamp': 1603539089,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除用户组\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/delete\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group3\\\"}\"\n",
    "access_token = '24.bc461ac1ea96b00a47a891199a3d6592.2592000.1606127896.282335-22870043'# 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
